#include <stdio.h> //f*(), printf(), perror()...
#include <sys/unistd.h> //getcwd(), get*id(), fork(), execlp()...
#include <sys/stat.h> //for the stat structure
#include <dirent.h> //defines d_type, opendir(), and so on
#include <time.h> //no warning when I don't include this, why?
#include <errno.h> //for the errno variable
#include <string.h> //for strerror

int main(int argc, char* argv[]) {
	struct dirent *e;
	struct stat s;
	struct tm *lt;
	DIR *d;
	char buf[1024], *p; //FIXME

	if (argc == 1)
		strcpy(buf, ".");
	else
		strcpy(buf, argv[1]);
	if (buf[strlen(buf)-1] != '/')
		strcat(buf, "/");
	p = buf + strlen(buf);
	d = opendir(buf);

	if (d) {
		while (e = readdir(d)) {
			if (*e->d_name == '.')
				continue; //ignored hidden files
			strcpy(p, e->d_name);
			if (stat(buf, &s) != 0)
				printf("NOSTAT inode %li, "
					"%s\n", 
					e->d_ino,
					e->d_name);
			else {
				lt = localtime(&s.st_mtime);
				printf("inode %li, "
					"nlinks %3li, "
					"length %7li, "
					"date %2d/%2d/%d %2d:%2d:%2d, "
					"%s\n", 
					e->d_ino,
					s.st_nlink,
					s.st_size,
					//date
					lt->tm_mday,
					lt->tm_mon+1,
					1900+lt->tm_year,
					//hour
					lt->tm_hour,
					lt->tm_min,
					lt->tm_sec,
					//name (no max len)
					e->d_name);
			}
		}
		closedir(d);
		return 0;
	}
	else {
		perror("diropen");
		return errno;
	}
}
